package com.aring.controller;

import java.util.ArrayList;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aring.bean.PlaceManager;
import com.aring.bean.SuperManager;
import com.aring.bean.User;
import com.aring.service.SuperManagerService;
import com.aring.service.UserService;

@Controller
public class SuperManagerController {

	@Autowired
	private SuperManagerService superService;
	@RequestMapping(value="/superLogin.do",method=RequestMethod.POST)
	public ModelAndView superLogin(String account,String password,HttpServletResponse response){
		ModelAndView mv=new ModelAndView("error");
		SuperManager manager=superService.selectSuperByAccount(Integer.valueOf(account));
		if(manager==null||!(manager.getPassword().trim().equals(password)))
		{
			mv.addObject("message", "账号或密码出错");mv.setViewName("error");return mv;
		}
		if(manager.getPassword().trim().equals(password)){
			Cookie cookie=new Cookie("super",account);
			response.addCookie(cookie);
			mv.setViewName("redirect:/SuperManager/superManager.jsp");
			return mv;
		}
		return mv;
	}
	public String judgeLogin(HttpServletRequest request){
		Cookie[] cookies=request.getCookies();
		String judge = null;
		for (int i = 0; i < cookies.length; i++) {
			if(cookies[i].getName().equals("super")) judge=cookies[i].getValue();
		}
		return judge;
	}
	@RequestMapping(value="/showManager.do")
	public ModelAndView showManager(HttpServletRequest request){
		ModelAndView model=new ModelAndView("redirect:/SuperManager/superLogin.jsp");
		String judge=judgeLogin(request);
		if(judge==null) return model;
		ArrayList<PlaceManager> list=new ArrayList<PlaceManager>();
		list=superService.selectManager(Integer.valueOf(judge));
		request.getSession().setAttribute("list", list);
		model.setViewName("redirect:/SuperManager/showManager.jsp");
		return model;
	}
	@RequestMapping(value="/editManager.do")
	public ModelAndView editManager(HttpServletRequest request,Integer manager_no)
	{
		ModelAndView model=new ModelAndView("redirect:/SuperManager/superLogin.jsp");
		String judge=judgeLogin(request);
		if(judge==null) return model;
		PlaceManager manager=superService.selectManagerByPm_no(Integer.valueOf(manager_no));
		model.setViewName("redirect:/SuperManager/editManager.jsp");
		request.getSession().setAttribute("manager", manager);
		return model;
		
		
	}
	
	@RequestMapping(value="/updateManager.do",method=RequestMethod.POST)
	public ModelAndView updateManager(HttpServletRequest request,String account,String password,String status,String pm_no){
		ModelAndView model=new ModelAndView("redirect:/SuperManager/superLogin.jsp");
		String judge=judgeLogin(request);
		if(judge==null) return model;
		PlaceManager manager=superService.selectManagerByPm_no(Integer.valueOf(pm_no));
		manager.setPassword(password);
		manager.setStatus(Integer.valueOf(status));
		if(1==superService.updateManager(manager))
		{
			model.setViewName("redirect:/SuperManager/superManager.jsp");
		}
		else model.setViewName("error");
		return model;
	} 
	@RequestMapping(value="/outSuperLogin.do")
	public String outSuperLogin(HttpServletRequest request,HttpServletResponse response)
	{
		Cookie[] cookies=request.getCookies();
    	if(cookies==null) return "redirect:/SuperManager/superLogin.jsp";
    	for (int i = 0; i < cookies.length; i++) {
		  cookies[i]=new Cookie(cookies[i].getName(),null);
		  cookies[i].setMaxAge(0);
		  response.addCookie(cookies[i]);
		}
    	return "redirect:/SuperManager/superLogin.jsp";
	}	
	
}